<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('发起请假申请')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-leave-edit" th:object="${sysOaLeave}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">
                <label class="col-sm-3 control-label">请假单号：</label>
                <div class="col-sm-8">
                    <input name="name" th:field="*{leaveNo}" class="form-control" type="text" readonly>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">请假名称：</label>
                <div class="col-sm-8">
                    <input name="name" th:field="*{name}" class="form-control" type="text" required>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">请假理由：</label>
                <div class="col-sm-8">
                    <textarea name="reason" class="form-control" required>[[*{reason}]]</textarea>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">开始时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="startTime" id="startTime" th:value="${#dates.format(sysOaLeave.startTime, 'yyyy-MM-dd HH:mm:ss')}" class=" datetime-input form-control" placeholder="yyyy-MM-dd" type="text" required>

                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">结束时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="endTime" id="endTime" th:value="${#dates.format(sysOaLeave.endTime, 'yyyy-MM-dd HH:mm:ss')}"  class="datetime-input form-control" placeholder="结束时间" type="text" required>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">请假时长：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="leaveTime" id="leaveTime" class="form-control" th:field="*{leaveTime}"  type="text" readonly>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">附件：</label>
                <div class="col-sm-8">
                    <input type="hidden" name="fileUrl" id="filekey" th:field="*{fileUrl}">
                    <div class="form-control-static">
                        <a id="fileName"></a>
                       <!-- <button id="preview">预览</button>-->
                    </div>
                    <div class="file-loading">
                        <input class="form-control file-upload" id="fileUrl"  name="file" type="file">
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <script th:inline="javascript">
        var prefix = ctx + "system/leave";
        $("#form-leave-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-leave-edit').serialize());
            }
        }

        $(function() {
            var filekey=$("#fileKey").val()
            var data= $.common.getFileByKey(filekey);
            console.log(JSON.stringify(data))
            $("#preview").click(function () {
                preview(data.url);
            })
        });
        //文件预览服务器
        function preview(url) {
            window.open('http://47.98.36.102:8012/onlinePreview?url='+encodeURIComponent(Base64.encode(url)));
        }
        $(".file-upload").each(function (i) {
            var val = $("input[name='" + this.id + "']").val()
            $(this).fileinput({
                'uploadUrl': '/common/upload',
                initialPreviewAsData: true,
                initialPreview: [val],
                maxFileCount: 1,
                autoReplace: true
            }).on('fileuploaded', function (event, data, previewId, index) {
                $("input[name='" + event.currentTarget.id + "']").val(data.response.fileKey)
            }).on('fileremoved', function (event, id, index) {
                $("input[name='" + event.currentTarget.id + "']").val('')
            })
            $(this).fileinput('_initFileActions');
        });



        function formatBetweenTime(date1,date2) {
            var date3 = date2.getTime() - date1.getTime() //时间差的毫秒数
            console.log(date3)
            var days=Math.floor(date3 / (24*3600*1000)) // 计算出相差天数
            //计算出小时数
            var leave1 = date3 % (24*3600*1000) //计算天数后剩余的毫秒数
            var hours = Math.floor(leave1 / (3600*1000))

            //计算出相差分钟数
            var leave2 = leave1 % (3600*1000) //计算小时数后剩余的毫秒数
            var minutes = Math.floor(leave2 / (60*1000))
            console.log(minutes)

            //计算出相差秒数
            var leave3 = leave2 % (60*1000) //计算分钟数后剩余的毫秒数
            var seconds = Math.round(leave3 / 1000)
            return days+"天"+hours+"小时"+minutes+"分钟"+seconds+"秒";
        }
        $(function() {
           var filekey=$("#filekey").val();
           var data= $.common.getFileByKey(filekey);
           $('#fileName').attr('href',"../../../common/download?fileKey="+filekey);
           $("#fileName").text(data.name);
        });
    </script>
    <script th:inline="javascript">
        $('#endTime').blur(function () {
            var startTime =$("#startTime").val();
            var endTime = $("#endTime").val();
            var formatBetweenTime1 = formatBetweenTime(new Date(startTime),new Date(endTime));
            $("#leaveTime").val(formatBetweenTime1);
        });
    </script>
</body>
</html>